//
//  CYCompanyRechargeVC.m
//  CreditAddressBookEE
//
//  Created by LE on 16/1/27.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYCompanyRechargeVC.h"
#import "CYDiscoveryHttpTool.h"
#import "MBProgressHUD+LE.h"
#import "CYCompanyUserTool.h"

@interface CYCompanyRechargeVC ()<UIWebViewDelegate>

@end

@implementation CYCompanyRechargeVC

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self companyRecharge];
    self.title = @"充值";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
/*!
 *  @brief  企业充值
 */
//- (void)companyRecharge {
//    [CYDiscoveryHttpTool companyRechargeWithParam:self.param success:^(NSString *html) {
//        [self setupWebView:html];
//    } failure:^(NSString *message, BOOL isLogin) {
//        [MBProgressHUD showError:message];
//        if (!isLogin) {
//            [self.navigationController popToRootViewControllerAnimated:NO];
//            [CYCompanyUserTool logoutWithController:self];
//        }
//    }];
//}

- (void)setupWebView:(NSString *)html {
    UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.frame];
    webView.delegate = self;
    [webView loadHTMLString:html baseURL:nil];
    [self.view addSubview:webView];
    //添加滑动返回
    UISwipeGestureRecognizer *rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
    rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    [webView addGestureRecognizer:rightSwipeGestureRecognizer];
}
/**
 *  添加滑动返回
 */
- (void)handleSwipes:(UISwipeGestureRecognizer *)sender {
    if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
        [self.navigationController popViewControllerAnimated:YES];
        [[self navigationController] setNavigationBarHidden:NO animated:NO];
    }
}

#pragma mark -
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
//    [self.navigationController setNavigationBarHidden:NO animated:YES];
//    [MBProgressHUD hideHUDForView:self.view animated:NO];
//    [MBProgressHUD showError:@"网络错误"];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    //如果加载完成webview后，navi隐藏,加载失败，提供系统导航栏给用户返回
//    [self.navigationController setNavigationBarHidden:YES animated:NO];
    [MBProgressHUD hideHUDForView:self.view];
    webView.backgroundColor = CYNaviThemeColorAndroid;
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    [MBProgressHUD hideHUDForView:self.view];
    NSString *url = request.URL.absoluteString;
    //h5内控件字段方法:searchinfo
    if([url hasSuffix:@"searchinfo"]) {
        //do something you want
        //返回上一级时，取消nav的隐藏
        [[self navigationController] setNavigationBarHidden:NO animated:NO];
        //js交互oc获取到h5内的返回按钮后，点击返回，popvc
        [self.navigationController popViewControllerAnimated:YES];
        return NO;
    }
    return YES;
}
@end
